package com.portal.gui.marketman;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Field;
import com.portal.logic.FieldDAO;
import com.portal.logic.SerializationUtil;

public class EditFieldScr extends JFrame {

	private JPanel contentPane;
	private JTextField txtEditField;

	public EditFieldScr() {
		setTitle("Edit Field");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		
		Connection connection = ConnectionManager.getConnection();
		final FieldDAO fieldDAO = new FieldDAO(connection) ;
		List<Field> fields = fieldDAO.selectAll();
		
		final JComboBox cmbFields = new JComboBox();
		
		Iterator<Field> iterator = fields.iterator();
		while (iterator.hasNext()) {
			Field field = (Field) iterator.next();
			cmbFields.addItem(field.getName());
		}
		
		cmbFields.setBounds(106, 92, 141, 25);
		contentPane.add(cmbFields);

		JLabel lblChooseFieldYou = new JLabel("Choose Field you want edit:");
		lblChooseFieldYou.setBounds(48, 23, 194, 37);
		contentPane.add(lblChooseFieldYou);

		txtEditField = new JTextField();
		txtEditField.setBounds(106, 161, 136, 22);
		contentPane.add(txtEditField);
		txtEditField.setColumns(10);

		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(48, 217, 97, 25);
		contentPane.add(btnBack);

		JButton btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String selectedField = (String)cmbFields.getSelectedItem();
				String editedField = txtEditField.getText();
				if(!"".equalsIgnoreCase(editedField)){
					Field field = fieldDAO.selectByName(selectedField);
					field.setName(editedField);
					
					try {
						SerializationUtil.serialize(field, "c:\\editField.out");
					} catch (IOException e) {
						e.printStackTrace();
					}
					
					fieldDAO.update();
					JOptionPane.showMessageDialog(null, "Field was updated!");
				}else{
					JOptionPane.showMessageDialog(null, "New value for Field can not be empty.");
				}
			}
		});
		btnUpdate.setBounds(293, 160, 97, 25);
		contentPane.add(btnUpdate);
		
		JLabel lblOldValue = new JLabel("Old Value");
		lblOldValue.setBounds(12, 96, 56, 16);
		contentPane.add(lblOldValue);
		
		JLabel lblNewValue = new JLabel("New Value");
		lblNewValue.setBounds(12, 164, 72, 16);
		contentPane.add(lblNewValue);
	}
}
